import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:video_player/video_player.dart';
import 'package:xeswx/application.dart';
import 'package:xeswx/components/rounded_box.dart';

/**
 * 
 * 启动屏页面，main启动后进入本页面
 */
class SplashPage extends StatefulWidget {
  @override
  _SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  VideoPlayerController _controller;

// 自定义字体，使用 customFont.copyWith来重新定义该属性。
  var customFont = TextStyle(
    fontFamily: "MaoTi", // 指定该Text的字体。
  );

  @override
  void initState() {
    print('splashPageInit');
    // TODO: implement initState
    super.initState();
    _controller = VideoPlayerController
        // .network('https://mvp.yanhuisou.com/uploads/files/20200307/fb5dbe8eb6d74d7ab75ba5bd5a845baf.mp4')
        .asset('src/start.mp4')
      ..setLooping(true)
      ..setVolume(0.3)
      ..initialize()
      .then((_) {
        // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
        _controller.play();
        setState(() {});
      });

    // _controller = VideoPlayerController.asset('src/start.mp4');
    //   // ..setLooping(true)
    //   // ..initialize().then((val) {
    //   //   print("video init");
    //   //   // _controller.play();
    //   //   setState(() {});
    //   // });
    //   _controller.addListener(() {
    //   setState(() {});
    // });
    // _controller.setLooping(true);
    // _controller.initialize().then((_) => setState(() {}));
    // _controller.play();
  }

  @override
  void dispose() {
    // TODO: implement dispose

    _controller.pause();
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334);

    final size = MediaQuery.of(context).size;
    Application.screenWidth = size.width;
    Application.screenHeight = size.height;
    Application.statusBarHeight = MediaQuery.of(context).padding.top;
    Application.bottomBarHeight = MediaQuery.of(context).padding.bottom;
    return Stack(children: [
      Container(
          child: _controller.value.initialized
              ? VideoPlayer(_controller)
              : Image(
                  image: AssetImage('src/bg.jpg'),
                  height: Application.screenHeight,
                )),
      Positioned(
          top: Application.statusBarHeight + 10,
          right: 20,
          child:
              // Text('sss'),
              RoundedBox(
            width: 80.0,
            height: 32.0,
            borderRadius: 16,
            child: Container(
                child: Text(
              '直接进入',
              style: TextStyle(
                  color: Colors.white,
                  fontSize: 14,
                  decoration: TextDecoration.none,
                  fontWeight: FontWeight.w100),
            )),
          )..bgColor = Colors.black26),
      Positioned(
          bottom: 30,
          width: ScreenUtil().setWidth(750),
          child: Container(
            // alignment: Alignment.center,
            // width: 750,
            // width: ,
            // color: Colors.black26,
            child: Column(children: [
              Container(
                padding: EdgeInsets.all(10),
                child: Text('学而思网校-仿',
                    style: customFont.copyWith(
                        color: Colors.white,
                        fontSize: 25,
                        fontWeight: FontWeight.w100,
                        decoration: TextDecoration.none,
                        letterSpacing: 4)
                    // TextStyle(
                    //     fontFamily: 'Ewert',
                    //     color: Colors.red,
                    //     fontSize: 25,
                    //     decoration: TextDecoration.none,
                    //     letterSpacing: 4),

                    ),
              ),
              Container(
                margin: EdgeInsets.fromLTRB(0, 20, 0, 20),
                child: RoundedBox(
                  width: 300.0,
                  height: 50.0,
                  borderRadius: 30,
                  child: Container(
                      child: Text(
                    '登录/注册',
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 25,
                        decoration: TextDecoration.none,
                        fontFamily: 'PingFang',
                        fontWeight: FontWeight.w200,
                        letterSpacing: 4),
                  )),
                )..bgColor = Colors.redAccent,
              ),
              Container(
                  padding: EdgeInsets.all(10),
                  child: RichText(
                    text: TextSpan(
                        style: TextStyle(
                          color: Colors.white70,
                        ),
                        text: "登录即代表你已阅读并同意",
                        children: [
                          TextSpan(
                              text: " 《用户协议》",
                              style: TextStyle(
                                color: Colors.redAccent,
                              ),
                              recognizer: TapGestureRecognizer()
                                ..onTap = () {
                                  toProtocolPage();
                                }),
                          TextSpan(
                              text: "《隐私协议》",
                              style: TextStyle(
                                color: Colors.redAccent,
                              ),
                              recognizer: TapGestureRecognizer()
                                ..onTap = () {
                                  toProtocolPage(isUserProtocol: false);
                                })
                        ]),
                  )),
            ]),
          ))
    ]);
  }

// 跳转协议页面
  void toProtocolPage({bool isUserProtocol = true}) {}
// 跳转到主页
  void toMainPage() {}
}
